package com.jintian.smart.kernel.core.jackson;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
import com.jintian.smart.kernel.core.domain.ITailEntity;

public class OrmDatabindMoudle extends SimpleModule {
    private static final long serialVersionUID = 1L;

    public OrmDatabindMoudle() {
        this.setSerializerModifier(new BeanSerializerModifier() {
            @Override
            public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
                if (serializer instanceof BeanSerializerBase && ITailEntity.class.isAssignableFrom(beanDesc.getBeanClass())) {
                    return new TailEntitySerializerExt((BeanSerializerBase) serializer);
                }
                return serializer;
            }
        });
        this.setDeserializerModifier(new BeanDeserializerModifier() {
            @Override
            public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
                                                          JsonDeserializer<?> deserializer) {
                if (deserializer instanceof BeanDeserializerBase && ITailEntity.class.isAssignableFrom(beanDesc.getBeanClass())) {
                    return new TailEntityDeserializer((BeanDeserializerBase) deserializer);
                }
                return deserializer;
            }
        });
    }
}
